﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using DataAccess.DB;
using DuocThaiBinh.Common;
using DuocThaiBinh.HtmlHelpers;
using DuocThaiBinh.HtmlHelpers.ExportToExcel;
using Newtonsoft.Json;
using PagedList;

namespace DuocThaiBinh.Controllers
{
    public class ThongKeController : BaseController
    {

        [Authorize(Roles = "Administrator,Staff")]
        public ActionResult Index()
        {
            CuaHang curCuaHang = GetCurrentCuaHang();
            ViewBag.KhoID = new SelectList(curCuaHang.Khoes.OrderBy(p => p.TenKho), "ID", "TenKho");
            return View();
        }

        [Authorize(Roles = "Administrator,Staff")]
        public JsonResult IndexGet(int id = 0)
        {
            var spiks = new List<SanPhamInKho>();
            if (id == 0)
            {
                return Json(new
                {
                    data = spiks.Select(x => new { MaSP = x.SanPham.MaSP, SoLuong = x.SoLuong, DonVi = x.SanPham.DonVi, GiaBan = x.SanPham.GiaBan, GiaNhap = x.SanPham.GiaNhap, ID = x.ID })
                }, JsonRequestBehavior.AllowGet);
            }

            spiks = db.SanPhamInKhoes.Where(spik => spik.KhoID == id).ToList();

            return Json(new
            {
                data = spiks.Select(x => new { MaSP = x.SanPham.MaSP, SoLuong = x.SoLuong, DonVi = x.SanPham.DonVi, GiaBan = x.SanPham.GiaBan, GiaNhap = x.SanPham.GiaNhap, ID = x.ID })
            }, JsonRequestBehavior.AllowGet);
        }

        [Authorize(Roles = "Administrator,Staff")]
        public ExcelResult Export(int id = 0)
        {
            var spiks = new List<SanPhamInKho>();
            if (id == 0)
            {
                return null;
            }

            spiks = db.SanPhamInKhoes.Where(spik => spik.KhoID == id).ToList();

            DataTable tablenew = new DataTable("ThongKeTonKho");
            tablenew.Columns.Add("Mã Sản Phẩm");
            tablenew.Columns.Add("Tên Sản Phẩm");
            tablenew.Columns.Add("Số lượng");
            tablenew.Columns.Add("Đơn vị");
            tablenew.Columns.Add("Giá nhập");
            tablenew.Columns.Add("Giá bán");
            tablenew.Columns.Add("Hạn sử dụng");

            foreach (SanPhamInKho spik in spiks)
            {
                DataRow newrow = tablenew.NewRow();
                newrow["Mã Sản Phẩm"] = spik.SanPham.MaSP;
                newrow["Tên Sản Phẩm"] = spik.SanPham.TenSP;
                newrow["Số lượng"] = spik.SoLuong;
                newrow["Đơn vị"] = spik.SanPham.DonVi;
                newrow["Giá nhập"] = spik.SanPham.GiaNhap;
                newrow["Giá bán"] = spik.SanPham.GiaBan;

                DateTime dt = Convert.ToDateTime(spik.SanPham.HSD);
                newrow["Hạn sử dụng"] = dt.ToString("dd/MM/yyyy");
                tablenew.Rows.Add(newrow);
            }

            string excelFilename = "Thống kê tồn kho " + DateTime.Now.ToString("yyyy_MM_dd") + ".xlsx";

            var stream = CreateExcelFile.CreateExcelDocument(tablenew, excelFilename);

            return new ExcelResult()
            {
                FileName = excelFilename,
                Stream = stream
            };

        }
    }
}